package com.jerry.ball;

import lombok.Getter;
import org.apache.commons.lang3.RandomUtils;

/**
 * @author wanggk
 * @Description
 * @date Created in 2021/9/9
 */
@Getter
public class Team {
    private final String player;
    private int score;
    private int attackTime;
    private int scoreTime;
    private int reboundTime;
    private double hitRate;

    private final String[] twoPointWords = {"力劈华山", "冲起来就是暴扣", "后仰跳投", "翻身跳投", "单手上篮"};
    private final String[] threePointWords = {"后撤三分抬手就进!", "bang!bang!bang!", "from downtown!!!", "step back and shot", "胯下接背后再拉回，蹦!"};
    private final String[] dribbleWords = {"开始瞎几把运!", "一顿操作猛如虎，一看还在原地杵!", "这笔打的一点不合理啊。", "给姚明啊!给姚明啊！", "要丢，要丢。"};

    public Team(String player) {
        this.player = player;
    }

    public boolean attack() {
        attackTime++;
        System.out.println(player + "拿球发起进攻");
        dribble();
        if ("詹姆斯".equals(player)) {
            return twoPoint();
        } else if ("库里".equals(player)) {
            return threePoint();
        }
        throw new RuntimeException("瞎几把传啥呢");
    }

    private void dribble() {
        if (ThreadUtils.isMain()) {
            ThreadUtils.sleep(3000);
        }
        String commentary = dribbleWords[RandomUtils.nextInt(0, 5)];
        System.out.println(player + commentary);
        if (ThreadUtils.isMain()) {
            ThreadUtils.sleep(2000);
        }
    }

    private boolean twoPoint() {
        score += 2;
        scoreTime++;
        String commentary = twoPointWords[RandomUtils.nextInt(0, 5)];
        System.out.println(player + commentary + "!总得分:" + score);
        return false;
    }

    private boolean threePoint() {
        if (getHitRate() < 0.666d) {
            score += 3;
            scoreTime++;
            String commentary = threePointWords[RandomUtils.nextInt(0, 5)];
            System.out.println(commentary + player + "三分远射得分,总得分:" + score);
            return false;
        } else {
            System.out.println(player + "三分打铁~");
            return true;
        }
    }

    public void addRebound() {
        System.out.println(player + "抢到篮板球...");
        reboundTime++;
    }

    public double getHitRate() {
        return hitRate = (double) scoreTime / (double) attackTime;
    }
}
